package com.cgy.service.Impl;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.cgy.entity.ZiYuan;
import com.cgy.repository.ziyuanRepository;
import com.cgy.service.ZiYuanService;
@Service
public class ZiYuanServiceImpl implements ZiYuanService {
@Autowired
private ziyuanRepository ziyuanRepository;

	@Override
	public Page<ZiYuan> findAllByNameZiyuan(ZiYuan z, Pageable pageable) {
		// TODO Auto-generated method stub
		return ziyuanRepository.findAll(get(z), pageable);
	}

	private Specification<ZiYuan> get(ZiYuan z) {
		// TODO Auto-generated method stub
		return new Specification<ZiYuan>() {

			@Override
			public Predicate toPredicate(Root<ZiYuan> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				Predicate predicate = criteriaBuilder.conjunction();//动态sql表达式
				List<Expression<Boolean>> exList = predicate.getExpressions();
				
				return predicate;
			}
		};
	}
	@Transactional
	@Override
	public ZiYuan addAndUpdateZiYuan(ZiYuan z) {
		// TODO Auto-generated method stub
		return ziyuanRepository.saveAndFlush(z);
	}

	@Transactional
	public Integer deleteZiyuan(Integer zid) {
		// TODO Auto-generated method stub
		return ziyuanRepository.deleteZiYuan(zid);
	}

	@Override
	public List<ZiYuan> findAllZiYuan() {
		// TODO Auto-generated method stub
		return ziyuanRepository.findAll();
	}

}
